package com.cloudbroker.bcs.platform.dav.middle.extservice.point;

import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.annotation.Resource;

import com.cloudbroker.bcs.csvc.uc.api.vo.request.LTBPointAddForm;
import com.cloudbroker.bcs.csvc.uc.api.vo.response.LTBPointChangeBackVo;
import com.cloudbroker.bcs.platform.dav.middle.extservice.ltb.LTBService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.cloudbroker.bcs.common.util.StringUtil;
import com.cloudbroker.bcs.common.util.ValueUtil;
import com.cloudbroker.bcs.platform.dav.common.constant.CacheKeys;
import com.cloudbroker.bcs.platform.dav.common.util.CacheHelper;
import com.cloudbroker.bcs.platform.dav.middle.service.TenantService;
import com.cloudbroker.bcs.platform.dav.middle.util.point.PointType;

@Service
public class PointAddService {
    
    @Resource
    private TenantService tenantService;

    @Resource
    private LTBService ltbService;
    
    private static final Logger LOGGER = LoggerFactory.getLogger(PointAddService.class);
    
    /**
     * 增加积分
     * @param mobile 手机号
     * @param pointType 积分类型
     * @param userId 用户中心ID
     * @param tenantId 租户ID
     */
    @Async
    public void addPoint(String mobile, PointType pointType, Long userId, Integer tenantId) {
        if (StringUtil.isBlank(mobile) || pointType == null) {
            return;
        }
        if (pointType.getDayNum() < 0) {
            LTBPointChangeBackVo ltbPointChangeBackVo = addPointLtb(mobile, pointType, userId, tenantId);
            if (ltbPointChangeBackVo != null && ltbPointChangeBackVo.getError_no().equals("0")) {
                LOGGER.info(pointType.getDesc() + "手机号：" + mobile + ",增加积分:" + pointType.getPoint());
            } else {
                LOGGER.info(pointType.getDesc() + "手机号：" + mobile + ",增加积分错误，调用领投宝出错！");
            }
            return;
        }
        String numStr = CacheHelper.getInstance().get(getPointKey(mobile, pointType));
        int num = ValueUtil.getInt(numStr, 0);
        if (num < pointType.getDayNum()) {
            LTBPointChangeBackVo ltbPointChangeBackVo = addPointLtb(mobile, pointType, userId, tenantId);
            if (ltbPointChangeBackVo != null && ltbPointChangeBackVo.getError_no().equals("0")) {
                LOGGER.info(pointType.getDesc() + "手机号：" + mobile + ",增加积分:" + pointType.getPoint());
                num++;
                CacheHelper.getInstance().set(getPointKey(mobile, pointType), String.valueOf(num), getSecond());
            } else {
                LOGGER.info(pointType.getDesc() + "手机号：" + mobile + ",增加积分错误，调用领投宝出错！");
            }
        }
    }

    private LTBPointChangeBackVo addPointLtb(String mobile, PointType pointType, Long userId, Integer tenantId) {
        Integer companyId = tenantService.getCompanyIdByTenantId(tenantId);
        LTBPointAddForm ltbPointAddForm = new LTBPointAddForm();
        ltbPointAddForm.setMobile(mobile);
        ltbPointAddForm.setCompany_id(ValueUtil.getString(companyId));
        ltbPointAddForm.setUser_id(userId);
        ltbPointAddForm.setOpera_credits(pointType.getPoint());
        return ltbService.funcAddPoint(ltbPointAddForm);
    }

    private static String getPointKey(String mobile, PointType pointType) {
        return CacheKeys.POINT_MOBILE_KEY_PREFIX + mobile + CacheKeys.SEPARATOR + pointType.getName();
    }
    
    /**
     * 获取今天还剩下多少秒
     * @return
     */
    private static int getSecond() {
        Calendar curDate = Calendar.getInstance();
        Calendar tommorowDate = new GregorianCalendar(curDate.get(Calendar.YEAR), curDate.get(Calendar.MONTH),
                curDate.get(Calendar.DATE) + 1, 0, 0, 0);
        return (int) (tommorowDate.getTimeInMillis() - curDate.getTimeInMillis()) / 1000;
    }
}
